#include <iostream>
#include <string>
#include <memory>
#include <vector>

// 设备控制统一接口
class DeviceControl {
    public:
    DeviceControl() {}
    virtual ~DeviceControl() {}

    public:
    virtual void TurnOn() = 0;  // 统一的设备打开接口
    virtual void TurnOff() = 0; // 统一的设备关闭接口
};

// 风扇设备控制接口
class Fan {
    public:
    Fan() {}
    ~Fan() {}

    public:
    void Open() {
        std::cout << "open fan" << std::endl;
    }
    void Close() {
        std::cout << "close fan" << std::endl;
    }
};

// 电视机设备控制接口
class TV {
    public:
    TV() {}
    ~TV() {}

    public:
    void Switch(bool value) {
        if(value) {
            std::cout << "open tv" << std::endl;
        }
        else {
            std::cout << "close tv" << std::endl;
        }
    }
};

// 要通过设备统一接口控制风扇和电视机，就要实现两个适配器
// 风扇适配器
class FanAdapter : public DeviceControl {
    public:
    FanAdapter() : fan_(std::make_shared<Fan>()) {}
    virtual ~FanAdapter() {}

    public:
    virtual void TurnOn() {
        fan_->Open();
    }

    virtual void TurnOff() {
        fan_->Close();
    }

    private:
    std::shared_ptr<Fan> fan_;
};

// 电视机适配器
class TvAdapter : public DeviceControl {
    public:
    TvAdapter ():tv_(std::make_shared<TV>()) {}
    virtual ~TvAdapter() {};

    public:
    virtual void TurnOn() {
        tv_->Switch(true);
    }

    virtual void TurnOff() {
        tv_->Switch(false);
    }
    private:
    std::shared_ptr<TV> tv_;
};

int main () {
    std::cout << "hello adapter pattern!!!" << std::endl;
    
    // 设备列表
    std::vector<std::shared_ptr<DeviceControl>> devices;
    // 添加设备
    devices.emplace_back(std::make_shared<FanAdapter>());
    devices.emplace_back(std::make_shared<TvAdapter>());

    std::cout << "open all devices" << std::endl;
    for(const auto& v : devices) {
        v->TurnOn();
    }

    std::cout << "close all devices" << std::endl;
    for(const auto& v : devices) {
        v->TurnOff();
    }

    return 0;
}